package com.hkfn.rfb.rfb.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hkfn.rfb.R;
import com.hkfn.rfb.common.activity.BaseActivity;
import com.hkfn.rfb.common.adapter.BaseRecyclerAdapter;
import com.hkfn.rfb.rfb.bean.RfbSystemSettingModel;

public class RfbSystemSettingAdapter extends BaseRecyclerAdapter<RfbSystemSettingModel> {

    private BaseActivity activity;

    public RfbSystemSettingAdapter(BaseActivity activity) {
        this.activity = activity;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(activity).inflate(R.layout.item_rfb_system_setting_list, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, RfbSystemSettingModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data, RealPosition);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{

        private RelativeLayout rl_item_content;
        private LinearLayout ll_system_title;
        private ImageView iv_system_icon;
        private TextView tv_system_title;
        private LinearLayout ll_param_setting;
        private TextView tv_param_name;
        private TextView tv_param_max_value;
        private TextView tv_param_min_value;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.rl_item_content = itemView.findViewById(R.id.rl_item_content);
        myViewHolder.ll_system_title = itemView.findViewById(R.id.ll_system_title);
        myViewHolder.iv_system_icon = itemView.findViewById(R.id.iv_system_icon);
        myViewHolder.tv_system_title = itemView.findViewById(R.id.tv_system_title);
        myViewHolder.ll_param_setting = itemView.findViewById(R.id.ll_param_setting);
        myViewHolder.tv_param_name = itemView.findViewById(R.id.tv_param_name);
        myViewHolder.tv_param_max_value = itemView.findViewById(R.id.tv_param_max_value);
        myViewHolder.tv_param_min_value = itemView.findViewById(R.id.tv_param_min_value);
    }

    private void setItemData(MyViewHolder viewHolder, RfbSystemSettingModel data, int position) {
        if ("0".equals(data.getType())){
            viewHolder.ll_system_title.setVisibility(View.VISIBLE);
            viewHolder.ll_param_setting.setVisibility(View.GONE);
            switch (data.getFromSys()){
                case "0":// 电池
                    viewHolder.iv_system_icon.setImageResource(R.mipmap.icon_rfb_battery);
                    viewHolder.tv_system_title.setText("电池");
                    break;
                case "1":// 温度
                    viewHolder.iv_system_icon.setImageResource(R.mipmap.icon_rfb_temperature);
                    viewHolder.tv_system_title.setText("温度");
                    break;
                case "2":// 气体
                    viewHolder.iv_system_icon.setImageResource(R.mipmap.icon_rfb_gas);
                    viewHolder.tv_system_title.setText("气体");
                    break;
            }
        }else {
            viewHolder.ll_system_title.setVisibility(View.GONE);
            viewHolder.ll_param_setting.setVisibility(View.VISIBLE);
            viewHolder.tv_param_name.setText(data.getValueName());
            viewHolder.tv_param_max_value.setText(data.getMaxValue() + data.getUnit());
            viewHolder.tv_param_min_value.setText(data.getMinValue() + data.getUnit());
        }
    }

}
